// 广告的路由
import adv from '@/views/adv.ejs';
import axios from '@/api/index.js';


async function getDatas(res,p,perpage){
    let { data: { result, pageCount } } = await axios.get("/admin/getDatas", {
        params: {
            p,
            perpage
        }
    });
    let baseURL = axios.defaults.baseURL;
    res.render(adv({ users: result, baseURL, pageCount }));
}
function addEvent(res){
    let addadv= document.querySelector(".addadv")
        let adduermodel = document.querySelector(".adduermodel");
        let mask = document.querySelector(".mask")
        let imgFileEle =  document.querySelector(".imgFile")
        addadv.onclick = function(){
            adduermodel.style.display = "block";
            mask.style.display ="block";
        }
        document.querySelector(".closebtn").onclick = function(){
            adduermodel.style.display = "none";
            mask.style.display ="none";
        }
        document.querySelector(".btn").onclick = async function(){
            
            let username = document.querySelector(".username").value
            let imgFile = imgFileEle.files[0];
            let form =new FormData();
            form.append("username",username);
            form.append("imgFile",imgFile)
            let {data} = await axios.post("/admin/addadv",form);
            console.log("aaaa",data)

            if(data.status === 1){
                alert("添加成功");
                adduermodel.style.display = "none";
                mask.style.display ="none";
                await getDatas(res);
                 addEvent(res);
            }else{
                alert("添加失败");
            }
        }
        imgFileEle.onchange = function () {
            console.log("改变了");
            let imgFile = this.files[0];
            let fileRader = new FileReader();
            fileRader.readAsDataURL(imgFile); 
            fileRader.onload = function () {
                let str = fileRader.result;
                let imgshowEle = document.querySelector(".imgshow");
                imgshowEle.innerHTML = "";
                let img = new Image();
                img.style.width = "100px";
                img.style.height = "100px";
                img.src = str;
                imgshowEle.appendChild(img);
            }
        }

        let aEles = document.querySelectorAll(".page a");
        aEles.forEach(item => {
            item.onclick = async function (e) {
                e.preventDefault();
                let p = this.getAttribute("p");
                localStorage.setItem("p",p);
                await getDatas(res, p);
                    addEvent(res);
            }
        })

    let delBtns = document.querySelectorAll(".delBtn");
    delBtns.forEach(btn => {
        btn.onclick = async function () {
            let sure = confirm("你确定要删除吗？")
            if (sure) {
                console.log("点击了删除按钮");
                let _id = this.getAttribute("myid");
                console.log(_id);
                let { data } = await axios.delete("/admin/delData", {
                    params: {
                        _id
                    }
                })
                if (data.status === 1) {
                    console.log("删除成功");
                    // 查看 localstroage里是否存在 页码 ，如果存在那么传递页码到后端查询数据
                    let p;
                    if(localStorage.getItem("p")){
                        p = localStorage.getItem("p");
                    }
                    await getDatas(res);
                    addEvent(res);
                } else {
                    console.log("删除失败");
                }
            }
        }
    })

    // let updates = document.querySelectorAll(".update");
    // updates.forEach(item=>{
    //     item.onclick = async function(){
    //         adduermodel.style.display = "block";
    //             mask.style.display ="block";

    //         let _id = this.getAttribute("myid");
    //         let username = document.querySelector(".username").value
    //         let imgFile = imgFileEle.files[0];
    //         let form =new FormData();
    //         form.append("username",username);
    //         form.append("imgFile",imgFile)
    //         form.append("_id",_id)
    //         let {data} = await axios.post("/admin/beData",form);

    //         if(data.status === 1){
    //             alert("添加成功");
    //             adduermodel.style.display = "none";
    //             mask.style.display ="none";
    //             await getDatas(res);
    //              addEvent(res);
    //         }else{
    //             alert("添加失败");
    //         }
    //     }
    // })



    document.querySelector(".loginout").onclick = function(){
        localStorage.removeItem("token")
        window.location.href = "/login";
    }     
}
export default function(router){
    router.route("/admin/adv", async (req,res)=>{
        await getDatas(res);
        addEvent(res);
        

    })

}